﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 战棋模拟器
{
    class Minion
    { //字段部分
        public Pool pool;
        public bool alive = true;
        public int Place;//在棋盘上的位置
        public string Name;
        public int Attack = 1;//攻击力
        public int Health = 1;//生命值
        public int Star = 1;//默认入人的血量
        public race Race;
        public enum race//种族
        {
            all = -1,//融合怪
            none = 0,//无种族
            beast = 1,//野兽
            mech = 2,//机械
            murloc = 3,//鱼人
            devil = 4,//恶魔
            dragon = 5//龙
        }


        public int magnet = 0;
        public int plants = 0;
        public int buff = 0;
        public int damage = 0;
        public int count = 0;
        public int startEffect = 0;

        public enum skill:int//关键字
        {
            poisonous = 0,//剧毒
            windwrath = 1,/*风怒*/
            taunt = 2,/*嘲讽*/
            divineshield = 3,//圣盾   
            reborn = 4,//复生
            superWindwrath = 5//超级风怒
        }


        public bool[] Skills = new bool[6];

        //因为要添加对二王的支持，增加一个字段full health,在初始化阶段赋值成他的health.
        public int fullHealth;



        //可拓展的构造器
        public Minion()
        {
            string[] ns = ToString().Split('.');
            Name = ns[ns.Length - 1];//类名作为name
            Race = race.none;
        }


        public void SetValue(int a, int h)
        {
            Attack = a;
            Health = h;
        }


        //测试用来快速设置属性和亡语值（目前支持31磁力和进化的植物）
        public void SetValue(int a, int h, params bool[] skls)
        {
            Attack = a;
            Health = h;
            for(int i = 0;i<6;i++)
            {
                Skills[i] = skls[i];
            }
        }

        public void SetValue(int a, int h, string s)
        {
            Attack = a;
            Health = h;
            if (s.Contains("剧毒"))
                Skills[0] = true;
            if (s.Contains("超级风怒"))
                Skills[5] = true;
            else if (s.Contains("风怒"))
                Skills[1] = true;
            if (s.Contains("嘲讽"))
                Skills[2] = true;
            if (s.Contains("圣盾"))
                Skills[3] = true;
            if (s.Contains("复生"))
                Skills[4] = true;
        }


        //测试用来快速设置植物和磁力亡语:
        public void SetDeathrattle(int plt, int mag)
        {
            plants = plt;
            magnet = mag;
        }

        //功能函数

        //回合开始时触发，重写时要同时修改函数和StartEffect字段。
        public virtual void Start()
        {
            return;
        }


        //事件函数
        public virtual void ShieldLost()
        {
            if(pool.board.Debug)
            {
                Console.WriteLine("{0}用圣盾抵挡了伤害",Name);
            }
            Skills[3] = false;
            foreach (Minion m in pool.places)
            {
                if (m == null)
                    break;
                m.OnShieldlost(this);
            }
        }



        //攻击
        public virtual void Doattack(Minion m)
        {
            //先改变状态，再结算死亡，再把死亡的送到死亡队列里去
            if(m.Attack>0)
            {
                if (Skills[3])
                    ShieldLost();
                else
                    Getdamaged(m.Attack);
                if (m.Skills[0])
                    pool.board.deadQ.Enqueue(this);
                if (pool.board.deadQ.Contains(this) && (m.Race == race.dragon || m.Race == race.all))
                    m.pool.OnDragonKill();
            }
            if (m.Skills[3])
                m.ShieldLost();
            else
                m.Getdamaged(Attack);
            if (Skills[0])
                pool.board.deadQ.Enqueue(m);
            if (pool.board.deadQ.Contains(m) && (Race == race.dragon || Race == race.all))
                pool.OnDragonKill();
        }



        //同测随从损失圣盾调用的函数
        public virtual void OnShieldlost(Minion m)
        {
            return;
        }



        //受到m点伤害
        public virtual void Getdamaged(int n)
        {
            if(Skills[3])
            {
                if (pool.board.Debug)
                {
                    Console.WriteLine("{0}损失了圣盾", Name);
                }
                ShieldLost();
                return;
            }
            Health -= n;
            if (Health <= 0)
                pool.board.deadQ.Enqueue(this);
            return;
        }


        //死亡
        public virtual void Die()
        {
            if (alive == false)
                return;
            alive = false;
            pool.DeathList.Add(this);//死亡队列，用于触发坎格尔的学徒等特效
            int level = 0;
            Minion HighestLevel = new Minion();//仅触发最高等级的瑞文戴尔男爵
            if(Place<=pool.current)
                pool.current--;
            if (pool.board.Debug)
                Console.WriteLine("{0}第{1}个随从死亡", pool.direction ? "上方" : "下方", Place);
            for(int i = Place;i<6;i++)
            {                
                pool.places[i] = pool.places[i + 1];
                if (pool.places[i]!=null)
                    pool.places[i].Place -= 1;
            }
            pool.places[6] = null;
            pool.minionCount--;
            List<Minion> Todo = new List<Minion>();
            foreach(Minion m in pool.places)
            {
                if (m == null)
                    break;
                Todo.Add(m);
                if(m.Name=="瑞文戴尔男爵"&&level==0)
                {
                    HighestLevel = m;
                    level = 1;
                }
                else if(m.Name == "瑞文戴尔男爵_金色"&&level<2)
                {
                    HighestLevel = m;
                    level = 2;
                }
            }
            foreach(Minion m in Todo)
            {
                if ((m.Name == "瑞文戴尔男爵" || m.Name == "瑞文戴尔男爵_金色") && m != HighestLevel)
                    continue;
                m.OnDie(this);
            }
            Todo = new List<Minion>();
            foreach (Minion m in pool.next.places)
            {
                if (m != null)
                    Todo.Add(m);
            }
            foreach (Minion m in Todo)
                m.OnEnemyDie(this);
            Deathrattle();
            if (Skills[4])
            {
                Minion n = NewOne();
                n.Skills[4] = false;
                n.Health = 1;
                pool.Spawn(pool, Place, n);
            }
            pool.board.Reap();
        }
        

        //亡语函数
        public virtual void Deathrattle()
        {
            for(int i = plants;i>0;i--)
            {
                pool.Spawn(pool, Place, new 植物());
                pool.Spawn(pool, Place, new 植物());
            }


            for(int i = magnet;i>0;i--)
            {
                pool.Spawn(pool, Place, new 微型机器人());
                pool.Spawn(pool, Place, new 微型机器人());
                pool.Spawn(pool, Place, new 微型机器人());
            }
        }        


        //当己方随从池中召唤了新随从时
        public virtual void OnSpawn(Minion m)
        {
            return;
        }


        //当对方随从池召唤了新随从时
        public virtual void OnEnemySpawn(Minion m)
        {
            return;
        }


        //当己方随从死亡时调用
        public virtual void OnDie(Minion m)
        {
            return;
        }


        //当对方随从死亡时调用
        public virtual void OnEnemyDie(Minion m)
        {
            return;
        }


        //获得一个初始版本的该随从
        public virtual Minion NewOne()
        {
            return new Minion();
        }


        //上战场前初始化光环
        public virtual void Initialize()
        {
            //支持二王而做的改动
            fullHealth = Health;
            if (pool.board.DeathWing)
                Attack += 3;
        }


        //返回一个完全相同的(目前可以实现)
        public virtual Minion Copy()
        {
            Minion duplicate = NewOne();
            duplicate.Name = Name;
            duplicate.SetValue(Attack, Health, Skills[0], Skills[1], Skills[2], Skills[3], Skills[4], Skills[5]);
            duplicate.SetDeathrattle(plants, magnet);
            return duplicate;
        }
    }
}
